home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / Libraries / MacPNG Library 1.02 / pngMacSrc 1.02 / png.1 / minigzip.c next >
C/C++ Source or Header  |  1996-03-08  |  5KB  |  225 lines

  1. /* minigzip.c -- simulate gzip using the zlib compression library
  2.  * Copyright (C) 1995 Jean-loup Gailly.
  3.  * For conditions of distribution and use, see copyright notice in zlib.h 
  4.  */
  5.  
  6. /*
  7.  * minigzip is a minimal implementation of the gzip utility. This is
  8.  * only an example of using zlib and isn't meant to replace the
  9.  * full-featured gzip. No attempt is made to deal with file systems
  10.  * limiting names to 14 or 8+3 characters, etc... Error checking is
  11.  * very limited. So use minigzip only for testing; use gzip for the
  12.  * real thing. On MSDOS, use only on file names without extension
  13.  * or in pipe mode.
  14.  */
  15.  
  16. /* $Id: minigzip.c,v 1.5 1995/05/03 17:27:11 jloup Exp $ */
  17.  
  18. #include <stdio.h>
  19. #include "zlib.h"
  20.  
  21. #ifndef __GO32__
  22. extern void exit  __P((int));
  23. #endif
  24. extern int unlink __P((const char *));
  25.  
  26. #ifdef STDC
  27. #  include <string.h>
  28. #endif
  29.  
  30. #if defined(MSDOS) || defined(OS2) || defined(WIN32)
  31. #  include <fcntl.h>
  32. #  include <io.h>
  33. #  define SET_BINARY_MODE(file) setmode(fileno(file), O_BINARY)
  34. #else
  35. #  define SET_BINARY_MODE(file)
  36. #endif
  37.  
  38. #define BUFLEN 4096
  39. #define MAX_NAME_LEN 1024
  40.  
  41. #define local static
  42. /* For MSDOS and other systems with limitation on stack size. For Unix,
  43.     #define local
  44.    works also.
  45.  */
  46.  
  47. char *prog;
  48.  
  49. void error           __P((char *msg));
  50. void gz_compress     __P((FILE   *in, gzFile out));
  51. void gz_uncompress   __P((gzFile in, FILE   *out));
  52. void file_compress   __P((char  *file));
  53. void file_uncompress __P((char  *file));
  54. void main            __P((int argc, char *argv[]));
  55.  
  56.  
  57.  
  58. /* ===========================================================================
  59.  * Display error message and exit
  60.  */
  61. void error( char *msg)
  62. {
  63.     fprintf(stderr, "%s: %s\n", prog, msg);
  64.     exit(1);
  65. }
  66.  
  67. /* ===========================================================================
  68.  * Compress input to output then close both files.
  69.  */
  70. void gz_compress(FILE   *in, gzFile out)
  71. {
  72.     local char buf[BUFLEN];
  73.     int len;
  74.     int err;
  75.  
  76.     for (;;) {
  77.         len = fread(buf, 1, sizeof(buf), in);
  78.         if (ferror(in)) {
  79.             perror("fread");
  80.             exit(1);
  81.         }
  82.         if (len == 0) break;
  83.  
  84.         if (gzwrite(out, buf, len) != len) error(gzerror(out, &err));
  85.     }
  86.     fclose(in);
  87.     if (gzclose(out) != Z_OK) error("failed gzclose");
  88. }
  89.  
  90. /* ===========================================================================
  91.  * Uncompress input to output then close both files.
  92.  */
  93. void gz_uncompress(gzFile in, FILE   *out)
  94. {
  95.     local char buf[BUFLEN];
  96.     int len;
  97.     int err;
  98.  
  99.     for (;;) {
  100.         len = gzread(in, buf, sizeof(buf));
  101.         if (len < 0) error (gzerror(in, &err));
  102.         if (len == 0) break;
  103.  
  104.         if (fwrite(buf, 1, len, out) != (uInt)len) error("failed fwrite");
  105.     }
  106.     if (fclose(out)) error("failed fclose");
  107.  
  108.     if (gzclose(in) != Z_OK) error("failed gzclose");
  109. }
  110.  
  111.  
  112. /* ===========================================================================
  113.  * Compress the given file: create a corresponding .gz file and remove the
  114.  * original.
  115.  */
  116. void file_compress(char  *file)
  117. {
  118.     local char outfile[MAX_NAME_LEN];
  119.     FILE  *in;
  120.     gzFile out;
  121.  
  122.     strcpy(outfile, file);
  123.     strcat(outfile, ".gz");
  124.  
  125.     in = fopen(file, "rb");
  126.     if (in == NULL) {
  127.         perror(file);
  128.         exit(1);
  129.     }
  130.     out = gzopen(outfile, "wb");
  131.     if (out == NULL) {
  132.         fprintf(stderr, "%s: can't gzopen %s\n", prog, outfile);
  133.         exit(1);
  134.     }
  135.     gz_compress(in, out);
  136.  
  137.     unlink(file);
  138. }
  139.  
  140.  
  141. /* ===========================================================================
  142.  * Uncompress the given file and remove the original.
  143.  */
  144. void file_uncompress(char  *file)
  145. {
  146.     local char buf[MAX_NAME_LEN];
  147.     char *infile, *outfile;
  148.     FILE  *out;
  149.     gzFile in;
  150.     int len = strlen(file);
  151.  
  152.     strcpy(buf, file);
  153.  
  154.     if (len > 3 && strcmp(file+len-3, ".gz") == 0) {
  155.         infile = file;
  156.         outfile = buf;
  157.         outfile[len-3] = '\0';
  158.     } else {
  159.         outfile = file;
  160.         infile = buf;
  161.         strcat(infile, ".gz");
  162.     }
  163.     in = gzopen(infile, "rb");
  164.     if (in == NULL) {
  165.         fprintf(stderr, "%s: can't gzopen %s\n", prog, infile);
  166.         exit(1);
  167.     }
  168.     out = fopen(outfile, "wb");
  169.     if (out == NULL) {
  170.         perror(file);
  171.         exit(1);
  172.     }
  173.  
  174.     gz_uncompress(in, out);
  175.  
  176.     unlink(infile);
  177. }
  178.  
  179.  
  180. /* ===========================================================================
  181.  * Usage:  minigzip [-d] [files...]
  182.  */
  183.  
  184. void main(int argc, char *argv[])
  185. {   int uncompr = 0;
  186.     gzFile file;
  187.  
  188.     prog = argv[0];
  189.     argc--, argv++;
  190.  
  191.     if (argc > 0) {
  192.         uncompr = (strcmp(*argv, "-d") == 0);
  193.         if (uncompr) {
  194.             argc--, argv++;
  195.         }
  196.     }
  197.     
  198.     if (argc == 0) {
  199. #if defined(MACOS)    /* RMF needed a file */
  200.         SysBeep(0);
  201. #else
  202.         SET_BINARY_MODE(stdin);
  203.         SET_BINARY_MODE(stdout);
  204.         if (uncompr) {
  205.             file = gzdopen(fileno(stdin), "rb");
  206.             if (file == NULL) error("can't gzdopen stdin");
  207.             gz_uncompress(file, stdout);
  208.         } else {
  209.             file = gzdopen(fileno(stdout), "wb");
  210.             if (file == NULL) error("can't gzdopen stdout");
  211.             gz_compress(stdin, file);
  212.         }
  213. #endif
  214.     } else {
  215.         do {
  216.             if (uncompr) {
  217.                 file_uncompress(*argv);
  218.             } else {
  219.                 file_compress(*argv);
  220.             }
  221.         } while (argv++, --argc);
  222.     }
  223.     exit(0);
  224. }
  225.